package com.lanou.bbs.service;

import com.lanou.bbs.dao.UserDao;
import com.lanou.bbs.entity.User;
import com.lanou.bbs.util.JDBCUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;

@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public boolean register(User user) {
        try {
            JDBCUtil.begin();
            boolean hasUsername = userDao.hasUsername(user.getUsername());
            if (hasUsername) {
                return false;
            } else {
                return userDao.register(user);
            }
        } finally {
            JDBCUtil.closeConn();
        }
    }

    public User login(User dbuser) throws SQLException {

        User user = userDao.myLogin(dbuser);
        if (user == null) {
            return null;
        }
        return user;
    }
}
